#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.Security;
using System.Text;
using System.Runtime.InteropServices;
using ComTypes = System.Runtime.InteropServices.ComTypes;

using Osh;

namespace OshHost
{
	public static class NativeInterfaces
	{
		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("B722BCCB-4E68-101B-A2BC-00AA00404770")]
		public interface IOleCommandTarget
		{
			[PreserveSig]
			int QueryStatus(IntPtr pguidCmdGroup, int cCmds, [In, Out] IntPtr prgCmds, [In, Out] IntPtr pCmdText);
			[PreserveSig]
			int Exec(IntPtr pguidCmdGroup, int nCmdID, int nCmdexecopt, [In, MarshalAs(UnmanagedType.LPArray)] object[] pvaIn, [In, Out, MarshalAs(UnmanagedType.LPArray)] object[] pvaOut);
		}

		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("BD3F23C0-D43E-11CF-893B-00AA00BDCE1A")]
		public interface IDocHostUIHandler
		{
			[PreserveSig] int ShowContextMenu([In, MarshalAs(UnmanagedType.U4)] int dwID,
				[In] Win32.RECT pt,
				[In, MarshalAs(UnmanagedType.Interface)] object pcmdtReserved,
				[In, MarshalAs(UnmanagedType.Interface)] object pdispReserved);
			
			[PreserveSig] int GetHostInfo([In, Out] Win32.DOCHOSTUIINFO info);
			
			//[PreserveSig] int ShowUI([In] int dwID,
			//    [In] UnsafeNativeMethods.IOleInPlaceActiveObject activeObject,
			//    [In] NativeMethods.IOleCommandTarget commandTarget,
			//    [In] UnsafeNativeMethods.IOleInPlaceFrame frame,
			//    [In] UnsafeNativeMethods.IOleInPlaceUIWindow doc);
			[PreserveSig] int ShowUI([In] int dwID,
				[In] IntPtr activeObject, [In] IntPtr commandTarget, [In] IntPtr frame, [In] IntPtr doc);
			
			[PreserveSig] int HideUI();
			[PreserveSig] int UpdateUI();
			[PreserveSig] int EnableModeless([In, MarshalAs(UnmanagedType.Bool)] bool fEnable);
			[PreserveSig] int OnDocWindowActivate([In, MarshalAs(UnmanagedType.Bool)] bool fActivate);
			[PreserveSig] int OnFrameWindowActivate([In, MarshalAs(UnmanagedType.Bool)] bool fActivate);
			
			//[PreserveSig] int ResizeBorder([In] NativeMethods.COMRECT rect,
			//    [In] UnsafeNativeMethods.IOleInPlaceUIWindow doc, bool fFrameWindow);
			[PreserveSig] int ResizeBorder([In] Win32.COMRECT rect,
				[In] IntPtr doc, bool fFrameWindow);
			
			[PreserveSig] int TranslateAccelerator(
				[In] ref Win32.MSG msg, [In] ref Guid group,
				[In] int nCmdID);
			
			[PreserveSig] int GetOptionKeyPath(
				[Out, MarshalAs(UnmanagedType.LPArray)] string[] pbstrKey,
				[In, MarshalAs(UnmanagedType.U4)] int dw);
			
			//[PreserveSig] int GetDropTarget(
			//    [In, MarshalAs(UnmanagedType.Interface)] UnsafeNativeMethods.IOleDropTarget pDropTarget,
			//    [MarshalAs(UnmanagedType.Interface)] out UnsafeNativeMethods.IOleDropTarget ppDropTarget);
			[PreserveSig] int GetDropTarget([In] IntPtr pDropTarget, out IntPtr ppDropTarget);
			
			[PreserveSig] int GetExternal([MarshalAs(UnmanagedType.Interface)] out object ppDispatch);
			
			[PreserveSig] int TranslateUrl([In, MarshalAs(UnmanagedType.U4)] int dwTranslate,
				[In, MarshalAs(UnmanagedType.LPWStr)] string strURLIn,
				[MarshalAs(UnmanagedType.LPWStr)] out string pstrURLOut);
			
			[PreserveSig] int FilterDataObject(ComTypes.IDataObject pDO, out ComTypes.IDataObject ppDORet);
		}

		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("3050F3F0-98B5-11CF-BB82-00AA00BDCE0B")]
		public interface ICustomDoc
		{
			void SetUIHandler([In, MarshalAs(UnmanagedType.Interface)] IDocHostUIHandler pUIHandler);
		}


		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("00000100-0000-0000-C000-000000000046")]
		public interface IEnumUnknown
		{
			[PreserveSig]
			int Next(int celt, out IntPtr rgelt, out int pceltFetched);
			[PreserveSig]
			int Skip(int celt);
			void Reset();
			void Clone(out IEnumUnknown ppenum);
		}

		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("0000011B-0000-0000-C000-000000000046")]
		public interface IOleContainer
		{
			[PreserveSig]
			int ParseDisplayName([MarshalAs(UnmanagedType.Interface)] object pbc,
				[MarshalAs(UnmanagedType.BStr)] string pszDisplayName,
				[Out, MarshalAs(UnmanagedType.LPArray)] int[] pchEaten,
				[Out, MarshalAs(UnmanagedType.LPArray)] object[] ppmkOut);
			[PreserveSig]
			int EnumObjects(int grfFlags, out IEnumUnknown ppenum);
			[PreserveSig]
			int LockContainer(bool fLock);
		}


		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
		[TypeLibType(TypeLibTypeFlags.FHidden)]
		[Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")]
		public interface DWebBrowserEvents2
		{
			[DispId(0x6a)]
			void DownloadBegin();
			[DispId(0x68)]
			void DownloadComplete();
		    [DispId(250)]
		    void BeforeNavigate2([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,
		        [In] ref object URL, [In] ref object flags, [In] ref object targetFrameName,
		        [In] ref object postData, [In] ref object headers, [In, Out] ref bool cancel);
		    [DispId(0xfb)]
		    void NewWindow2([In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object pDisp, [In, Out] ref bool cancel);
		    [DispId(0x111)]
		    void NewWindow3([In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object pDisp, [In, Out] ref bool cancel,
				[In] ref object flags, [In] ref object urlContext, [In] ref object url);
		    [DispId(0xfc)]
		    void NavigateComplete2([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL);
		    [DispId(0x103)]
		    void DocumentComplete([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL);
			
			// ...
		}

		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("0000010d-0000-0000-C000-000000000046")]
		public interface IViewObject
		{
			[PreserveSig] int Draw(uint dwDrawAspect, int lindex, IntPtr pvAspect,
				[In] Win32.DVTARGETDEVICE ptd, IntPtr hdcTargetDev, IntPtr hdcDraw,
				[In] Win32.COMRECT lprcBounds, [In] Win32.COMRECT lprcWBounds,
				IntPtr pfnContinue, [In] int dwContinue);
			[PreserveSig] int GetColorSet(uint dwDrawAspect, int lindex, IntPtr pvAspect,
			    [In] Win32.DVTARGETDEVICE ptd, IntPtr hicTargetDev,
			    [Out] Win32.LOGPALETTE ppColorSet);
			[PreserveSig] int Freeze(uint dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);
			[PreserveSig] int Unfreeze(uint dwFreeze);
			void SetAdvise(int dwAspect, int advf,
				[In, MarshalAs(UnmanagedType.Interface)] ComTypes.IAdviseSink pAdvSink);
			void GetAdvise(
				[In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects,
			    [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf,
			    [In, Out, MarshalAs(UnmanagedType.LPArray)] ComTypes.IAdviseSink[] pAdvSink);
		}
 
		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
		public interface IServiceProvider
		{
			[PreserveSig] int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject);
		}

		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("79eac9ee-baf9-11ce-8c82-00aa004ba90b")]
		public interface IInternetSecurityManager
		{
			[PreserveSig] unsafe int SetSecuritySite(void* pSite);
			[PreserveSig] unsafe int GetSecuritySite(void** ppSite);
			[PreserveSig] unsafe int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, int* pdwZone, [In] int dwFlags);
			[PreserveSig] unsafe int GetSecurityId([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, byte* pbSecurityId, int* pcbSecurityId, int dwReserved);
			[PreserveSig] unsafe int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, int dwAction, byte* pPolicy, int cbPolicy, byte* pContext, int cbContext, int dwFlags, int dwReserved);
			[PreserveSig] unsafe int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, void* guidKey, byte** ppPolicy, int* pcbPolicy, byte* pContext, int cbContext, int dwReserved);
			[PreserveSig] int SetZoneMapping(int dwZone, [In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, int dwFlags);
			[PreserveSig] unsafe int GetZoneMappings(int dwZone, void** ppenumString, int dwFlags);
		}

		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
		public interface IObjectSafety
		{
			[PreserveSig] int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions);
			[PreserveSig] int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);

		}

		[ComImport]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		[Guid("B196B283-BAB4-101A-B69C-00AA00341D07")]
		public interface IProvideClassInfo
		{
			//[return: MarshalAs(UnmanagedType.Interface)] UnsafeNativeMethods.ITypeInfo GetClassInfo();
			IntPtr GetClassInfo();
		}

		[ComImport]
		[SuppressUnmanagedCodeSecurity]
		[TypeLibType(TypeLibTypeFlags.FOleAutomation | TypeLibTypeFlags.FDual | TypeLibTypeFlags.FHidden)]
		[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
		[Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E")]
		public interface IWebBrowser2
		{
			[DispId(-550)]
			void Refresh();
			[DispId(0xcb)]
			object Document { [return: MarshalAs(UnmanagedType.IDispatch)] get; }
			[DispId(0x227)]
			bool Silent { get; set; }

			// ...
		}

		[ComImport]
		[SuppressUnmanagedCodeSecurity]
		[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
		[Guid("3050F1FF-98B5-11CF-BB82-00AA00BDCE0B")]
		internal interface IHTMLElement
		{
			// ...
		}
 
		[ComImport]
		[SuppressUnmanagedCodeSecurity]
		[Guid("626FC520-A41E-11cf-A731-00A0C9082637")]
		[InterfaceType(ComInterfaceType.InterfaceIsDual)]
		internal interface IHTMLDocument
		{
			[return: MarshalAs(UnmanagedType.IDispatch)]
			object GetScript();
		}

		[ComImport]
		[SuppressUnmanagedCodeSecurity]
		[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
		[Guid("332C4425-26CB-11D0-B483-00C04FD90119")]
		public interface IHTMLDocument2
		{
			[DispId(0x000003FA)]
			string ReadyState { [return: MarshalAs(UnmanagedType.BStr)] get; }
			[DispId(0x0000040a)]
			object ParentWindow { [return: MarshalAs(UnmanagedType.IDispatch)] get; }
			
			// ...
		}

		[ComImport]
		[SuppressUnmanagedCodeSecurity]
		[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
		[Guid("3050F485-98B5-11CF-BB82-00AA00BDCE0B")]
		internal interface IHTMLDocument3
		{
			[DispId(DISPID.DISPID_IHTMLFRAMEBASE2_CONTENTWINDOW)]
			[return: MarshalAs(UnmanagedType.IDispatch)]
			IHTMLElement GetElementById([MarshalAs(UnmanagedType.BStr)] string id);
			
			// ...
		}

		[ComImport]
		[SuppressUnmanagedCodeSecurity]
		[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
		[Guid("332C4427-26CB-11D0-B483-00C04FD90119")]
		public interface IHTMLWindow2
		{
			[DispId(0x47f)]
			IHTMLDocument2 Document { get; }

			[DispId(0x48d)]
			[return: MarshalAs(UnmanagedType.Struct)]
			object ExecScript([In] string code, [In] string language);

			[DispId(1152)]
			IHTMLEventObj Event { get; }
			// ...
		}

		[ComImport]
		[Guid("3050F32D-98B5-11CF-BB82-00AA00BDCE0B")]
		[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
		public interface IHTMLEventObj
		{
			// ...
		}

		[ComImport]
		[Guid("3050F80B-98B5-11CF-BB82-00AA00BDCE0B")]
		[InterfaceType(ComInterfaceType.InterfaceIsDual)]
		public interface IHTMLDOMNode2
		{
			[DispId(-2147416999)]
			object OwnerDocument { [return: MarshalAs(UnmanagedType.IDispatch)] get; }
		}

		private static class DISPID
		{
			public const int DISPID_NORMAL_FIRST             = 1000;
			public const int DISPID_OMDOCUMENT               = DISPID_NORMAL_FIRST;

			public const int DISPID_XOBJ_MIN                 = unchecked((int)0x80010000);
			public const int DISPID_XOBJ_MAX                 = unchecked((int)0x8001FFFF);
			public const int DISPID_XOBJ_BASE                = DISPID_XOBJ_MIN;
			public const int DISPID_HTMLOBJECT               = DISPID_XOBJ_BASE   + 500;
			public const int DISPID_ELEMENT                  = DISPID_HTMLOBJECT  + 500;
			public const int DISPID_SITE                     = DISPID_ELEMENT     + 1000;
			public const int DISPID_OBJECT                   = DISPID_SITE        + 1000;
			public const int DISPID_STYLE                    = DISPID_OBJECT      + 1000;
			public const int DISPID_ATTRS                    = DISPID_STYLE       + 1000;
			public const int DISPID_EVENTS                   = DISPID_ATTRS       + 1000;
			public const int DISPID_XOBJ_EXPANDO             = DISPID_EVENTS      + 1000;
			public const int DISPID_XOBJ_ORDINAL             = DISPID_XOBJ_EXPANDO+ 1000;

			public const int DISPID_FRAMESITE                = DISPID_SITE        + 1000;

			public const int DISPID_EVPROP_ONLOAD                = DISPID_EVENTS + 32;
			public const int DISPID_EVPROP_ONREADYSTATECHANGE    = DISPID_EVENTS + 25;

			public const int DISPID_A_FIRST                  = DISPID_ATTRS;
			public const int DISPID_A_READYSTATE             = DISPID_A_FIRST + 116;
			public const int DISPID_A_ALLOWTRANSPARENCY      = DISPID_A_FIRST + 206;


			// IHTMLDocument3

			public const int DISPID_IHTMLDOCUMENT3_GETELEMENTBYID                      = DISPID_OMDOCUMENT + 88;

			// IHTMLFrameBase2

			public const int DISPID_IHTMLFRAMEBASE2_CONTENTWINDOW                      = DISPID_FRAMESITE + 9;
			public const int DISPID_IHTMLFRAMEBASE2_ONLOAD                             = DISPID_EVPROP_ONLOAD;
			public const int DISPID_IHTMLFRAMEBASE2_ONREADYSTATECHANGE                 = DISPID_EVPROP_ONREADYSTATECHANGE;
			public const int DISPID_IHTMLFRAMEBASE2_READYSTATE                         = DISPID_A_READYSTATE;
			public const int DISPID_IHTMLFRAMEBASE2_ALLOWTRANSPARENCY                  = DISPID_A_ALLOWTRANSPARENCY;
		}

		
		[ComImport]
		[SuppressUnmanagedCodeSecurity]
		[InterfaceType(ComInterfaceType.InterfaceIsDual)]
		[Guid("3050F6DB-98B5-11CF-BB82-00AA00BDCE0B")]
		public interface IHTMLFrameBase2
		{
			[DispId(DISPID.DISPID_IHTMLFRAMEBASE2_CONTENTWINDOW)]
			IHTMLWindow2 ContentWindow { get; }

			[DispId(DISPID.DISPID_IHTMLFRAMEBASE2_ONLOAD)]
			object Onload { get; set; }

			[DispId(DISPID.DISPID_IHTMLFRAMEBASE2_ONREADYSTATECHANGE)]
			object Onreadystatechange { get; set; }

			[DispId(DISPID.DISPID_IHTMLFRAMEBASE2_READYSTATE)]
			string ReadyState { [return: MarshalAs(UnmanagedType.BStr)] get; }

			[DispId(DISPID.DISPID_IHTMLFRAMEBASE2_ALLOWTRANSPARENCY)]
			bool AllowTransparency { get; set; }
		}
	}
}
